﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class TeachableSubjectViewModel : INotifyPropertyChanged
    {
        public TeachableSubjectViewModel()
        {
            RefreshMonhocGiangday();
            RefreshMonhoc();
            this.PropertyChanged += (s, e) => {
                if (insertCurMonhocGiangday != null)
                    insertCurMonhocGiangday.RaiseCanExecuteChanged();
                if (saveCurMonhocGiangday != null)
                    saveCurMonhocGiangday.RaiseCanExecuteChanged();
                if (delCurMonhocGiangday != null)
                    delCurMonhocGiangday.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<MONHOCGIANGDAY> monhocGiangdays;
        public IEnumerable<MONHOCGIANGDAY> MonhocGiangdays
        {
            get { return this.monhocGiangdays; }
            set
            {
                this.monhocGiangdays = value;
                OnPropertyChange("MonhocGiangdays");
            }
        }

        private IEnumerable<MONHOC> monhocs;
        public IEnumerable<MONHOC> Monhocs
        {
            get { return this.monhocs; }
            set
            {
                this.monhocs = value;
                OnPropertyChange("Monhocs");
            }
        }
        private MONHOC currentMonhoc;
        public MONHOC CurrentMonhoc
        {
            get { return this.currentMonhoc; }
            set
            {
                this.currentMonhoc = value;
                OnPropertyChange("CurrentMonhoc");
            }
        }
        private MONHOCGIANGDAY currentMonhocGiangday= new MONHOCGIANGDAY();
        public MONHOCGIANGDAY CurrentMonhocGiangday
        {
            get { return this.currentMonhocGiangday; }
            set
            {
                this.currentMonhocGiangday = value;
                OnPropertyChange("CurrentMonhocGiangday");
                try
                {
                    this.CurrentMonhoc = this.CurrentMonhocGiangday.MONHOC;
                }
                catch { }
            }
        }
        private IEnumerable<GIANGVIEN> giangviens;
        public IEnumerable<GIANGVIEN> Giangviens
        {
            get { return this.giangviens; }
            set
            {
                this.giangviens = value;
                OnPropertyChange("Giangviens");
            }
        }

        private static GIANGVIEN currentGiangvien= new GIANGVIEN();
        public static GIANGVIEN CurrentGiangvien 
        {
            get { return currentGiangvien; }
            set
            {
                currentGiangvien = value;
            }
        }
       
        #endregion

        #region refreshMonhocGiangday
        public void refresh()
        {
            RefreshMonhocGiangday();
            RefreshMonhoc();
        }

        private void RefreshMonhocGiangday()
        {
            this.ServiceClient.getAllMonhocGiangdayCompleted += (s, e) =>
            {
                this.MonhocGiangdays = e.Result;
            };
            this.ServiceClient.getAllMonhocGiangdayAsync(currentGiangvien.GiangVienID);
        }

        private void RefreshMonhoc()
        {
            this.ServiceClient.getAllMonhocCompleted += (s, e) =>
            {
                this.Monhocs = e.Result;
            };
            this.ServiceClient.getAllMonhocAsync();
        }

        private void RefreshGiangvien()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllGiangvienCompleted += (s, e) =>
            {
                this.giangviens = e.Result;
            };
            this.ServiceClient.getAllGiangvienAsync();
            

        }
        #endregion
        
        #region Save
        private RelayCommand saveCurMonhocGiangday;
        public ICommand SaveCurMonhocGiangday
        {
            get { return saveCurMonhocGiangday ?? (saveCurMonhocGiangday = new RelayCommand(() => SaveMonhocGiangday(), () => this.currentMonhocGiangday != null && this.currentMonhocGiangday.ID != 0)); }
        }

        private void SaveMonhocGiangday()
        {
            ServiceClient.saveMonhocGiangdayAsync(currentMonhocGiangday);
            RefreshMonhocGiangday();
        }

        
        #endregion

        #region del
        private RelayCommand delCurMonhocGiangday;
        public ICommand DelCurMonhocGiangday
        {
            get { return delCurMonhocGiangday ?? (delCurMonhocGiangday = new RelayCommand(() => DelMonhocGiangday(), () => this.currentMonhocGiangday != null && this.currentMonhocGiangday.ID != 0)); }
        }

        private void DelMonhocGiangday()
        {
            ServiceClient.deleteMonhocGiangdayAsync(currentMonhocGiangday);
            RefreshMonhocGiangday();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurMonhocGiangday;
        public ICommand InsertCurMonhocGiangday
        {
            get { return insertCurMonhocGiangday ?? (insertCurMonhocGiangday = new RelayCommand(() => InsertMonhocGiangday(), () => this.currentMonhocGiangday != null)); }
        }

        private void InsertMonhocGiangday()
        {
            this.CurrentMonhocGiangday.GiangVienID = currentGiangvien.GiangVienID;
            this.CurrentMonhocGiangday.MonHocID = this.CurrentMonhoc.ID;
            ServiceClient.insertMonhocGiangdaynAsync(CurrentMonhocGiangday);
            RefreshMonhocGiangday();
        }
        #endregion
        
    }
}
